package view;

import android.app.Activity;
import android.content.Context;
import android.graphics.PixelFormat;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;

import com.example.xuegengwang.xuegengwang.R;


/**
 * Created by Administrator on 2017/4/6.
 * 弹窗
 */

public class WaitingDialog {
    private static MyWaitingDialog mView = null;
    private static WindowManager mWindowManager = null;
    public static Boolean isShown = false;

    public static void showWaitingDialog(final Context context) {
        Handler handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                if (isShown) {
                    return;
                } else {
                    isShown = true;
                    mView = (MyWaitingDialog) LayoutInflater.from(context).inflate(R.layout.waitting_layout, null);
                    mWindowManager = ((Activity)context).getWindowManager();
                    final WindowManager.LayoutParams params = new WindowManager.LayoutParams();
                    params.type = WindowManager.LayoutParams.TYPE_TOAST;
                    params.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
                    params.format = PixelFormat.TRANSLUCENT;
                    params.width = WindowManager.LayoutParams.MATCH_PARENT;
                    params.height = WindowManager.LayoutParams.WRAP_CONTENT;
                    params.gravity = Gravity.CENTER;
                    mWindowManager.addView(mView, params);
                    ImageView waiting_ima = (ImageView) mView.findViewById(R.id.waiting_ima);
                    Animation an = new RotateAnimation(0.0f, +350.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
                    an.setDuration(3000);
                    an.setRepeatCount(Animation.INFINITE);
                    an.setRepeatMode(Animation.RESTART);
                    an.setInterpolator(new LinearInterpolator());
                    waiting_ima.startAnimation(an);
                    mView.setOnBackDown(new MyWaitingDialog.onBackDown() {
                        @Override
                        public void onBackDown() {
                            if (isShown && null != mView) {
                                mWindowManager.removeView(mView);
                                isShown = false;
                                mView = null;
                                mWindowManager = null;
                            }
                        }
                    });
                }
            }
        };
        Message message = new Message();
        handler.sendMessage(message);
    }

    public static void closeWaitingDialog() {
        if (isShown && null != mView) {
            mWindowManager.removeView(mView);
            isShown = false;
            mView = null;
            mWindowManager = null;
        }
    }


}
